//
//  DetailViewController.m
//  bh
//
//  Created by lanou on 15/12/19.
//  Copyright © 2015年 lanou. All rights reserved.
//

#import "DetailViewController.h"
#import "LORequestManger.h"
#import "DetailModel.h"
#import "ProfessorModel.h"
#import "DetailScrollView.h"
#import "BrowseModel.h"
#import "UIImageView+AFNetworking.h"
#import "WorkTableViewCell.h"
#import "BrowseThreeTableViewCell.h"

@interface DetailViewController ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>

// 存放 BrowseModel
@property (nonatomic, strong) NSMutableArray *array;

// 存放 DetailModel
@property (nonatomic, strong) NSMutableArray *array2;

// 存放 DetailModel.service
@property (nonatomic, strong) NSMutableArray *array3;

// 存放 DetailModel.price
@property (nonatomic, strong) NSMutableArray *array4;

@property (nonatomic, strong) DetailModel *detailModel;

@property (nonatomic, strong) UISegmentedControl *segmentedControl;

@property (nonatomic, strong) BrowseModel *browseModel;


@end

@implementation DetailViewController


- (NSMutableArray *)array
{
    if (!_array) {
        _array = [[NSMutableArray alloc] init];
    }
    return _array;
}

- (NSMutableArray *)array2
{
    if (!_array2) {
        _array2 = [[NSMutableArray alloc] init];
    }
    return _array2;
}

- (NSMutableArray *)array3
{
    if (!_array3) {
        _array3 = [[NSMutableArray alloc] init];
    }
    return _array3;
}

- (NSMutableArray *)array4
{
    if (!_array4) {
        _array4 = [[NSMutableArray alloc] init];
    }
    return _array4;
}

- (void)jsonDetailUrl {
    
    NSString *str = [NSString stringWithFormat:@"http://api.meilijia.com/app/user_home_page?user_id=%@", self.professorModel.user_id];
    
    [LORequestManger GET:str success:^(id response) {
        
        NSDictionary *dic = (NSDictionary *)response;
        
        NSDictionary *dataDict = dic[@"data"];
        
        
        NSDictionary *user_home_work_listDict = dataDict[@"user_home_work_list"];
       
        
        NSArray *listArray = user_home_work_listDict[@"collections"];
        for (NSDictionary *dic in listArray) {
            BrowseModel *model = [[BrowseModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.array addObject:model];
            
        }
        
        self.detailScrollView.contentSize = CGSizeMake(kWidth, self.array.count * (kHeight / 2 + 110) + kHeight + 70);
        
        [_detailScrollView.tableView setFrame:(CGRectMake(0, kHeight - 20, kWidth, (kHeight / 2 + 110) * self.array.count + 100))];
        

        NSDictionary *user_home_infoDict = dataDict[@"user_home_info"];
        
        NSArray *array = user_home_infoDict[@"pro_service_new"];
        
        
        _detailModel = [[DetailModel alloc] init];
        
        _detailModel.pro_serviceDic = user_home_infoDict[@"pro_service"];
        _detailModel.pro_service_newArray = user_home_infoDict[@"pro_service_new"];
        _detailModel.user_infoDic = user_home_infoDict[@"user_info"];
        
        _detailModel.stylesArray = _detailModel.pro_serviceDic[@"styles"];
        
        if (_c == 0) {
            _detailScrollView.personLabel1.text = _detailModel.pro_serviceDic[@"bio"];
            
            _detailScrollView.styleLabel1.text = [NSString stringWithFormat:@"%@, %@, %@, %@, %@",_detailModel.stylesArray[0],_detailModel.stylesArray[1], _detailModel.stylesArray[2], _detailModel.stylesArray[3], _detailModel.stylesArray[4]];
        }
        
        for (NSDictionary *dic in array) {
            DetailModel *model = [[DetailModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            NSString *str = [NSString stringWithFormat:@"￥%@ - %@ /平米", model.min, model.max];
            [self.array4 addObject:str];
            [self.array3 addObject:model.service];
            
            [self.array2 addObject:model];
        }
        
         NSDictionary *proDict = user_home_infoDict[@"pro_service"];
        
        if (_c == 1) {
            self.detailScrollView.designLabel1.text = proDict[@"native_place"];
            NSArray  *array = proDict[@"services"];
            self.detailScrollView.personLabel1.text = [NSString stringWithFormat:@"%@, %@, %@", array[0], array[1], array[2]];
            self.detailScrollView.styleLabel1.text = proDict[@"exp_years"];
        }
        
        [self.detailScrollView.tableView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];


}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self jsonDetailUrl];
    
    self.view.backgroundColor = [UIColor grayColor];
    

    self.detailScrollView = [[DetailScrollView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight)];

    self.detailScrollView.userInteractionEnabled = YES;
    
    [self.detailScrollView setTableViewTarget:self];
    
    
    [self.detailScrollView.pictureView setImageWithURL:[NSURL URLWithString:_professorModel.user_face]];
    
    self.detailScrollView.nameLabel.text = _professorModel.user_nick;
    
    self.detailScrollView.contactLabel.text = _professorModel.verified_work_count;
    
    self.detailScrollView.reviewLabel.text = _professorModel.contacted_count;

            if (_c == 0) {
                
                self.detailScrollView.professorLabel.text = @"设计师";
                
                self.detailScrollView.contactLabel1.text = @"套作品";
                
                [self.detailScrollView.button1 setTitle:@"找我设计" forState:(UIControlStateNormal)];
                self.detailScrollView.styleLabel.text = @"擅长风格";
                self.detailScrollView.designLabel.text = @"设计收费";
                self.detailScrollView.personLabel.text = @"个人宣言";

                self.detailScrollView.designLabel1.text = [NSString stringWithFormat:@"￥%@ - %@ /平米", _professorModel.pro_price_min, _professorModel.pro_price_max];
                
            } else {
                self.detailScrollView.professorLabel.text = @"工长";
                self.detailScrollView.contactLabel1.text = @"个施工项目";
               [self.detailScrollView.button1 setTitle:@"找我施工" forState:(UIControlStateNormal)];
                self.detailScrollView.styleLabel.text = @"施工经验";
                self.detailScrollView.designLabel.text = @"籍贯";
                self.detailScrollView.personLabel.text = @"施工方式";

            }

    
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 30, 20, 20)];
    [backButton setImage:[UIImage imageNamed:@"f"] forState:(UIControlStateNormal)];
    [backButton addTarget:self action:@selector(backToProfessor) forControlEvents:(UIControlEventTouchUpInside)];
    
    [_detailScrollView.personalView addSubview:backButton];
    
    self.detailScrollView.serviceLabel1.text = _professorModel.location;

    
    [self.view addSubview:self.detailScrollView];
    
    
    [self.detailScrollView.tableView registerClass:[BrowseThreeTableViewCell class] forCellReuseIdentifier:@"cell11"];
    
    [self.detailScrollView.tableView registerClass:[WorkTableViewCell class] forCellReuseIdentifier:@"cell12"];
    
    
    // Do any additional setup after loading the view.
}

- (void)backToProfessor {
    
    [self dismissViewControllerAnimated:self completion:nil];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    if (_c == 0) {
        return 2;
    } else {
        return 1;
    }
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (_c == 0) {
        if (section == 0) {
            return 3;
        } else {
            return self.array.count;
        }
    } else {
        return self.array.count;
    }
    
    
   
    
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
    if (_c == 0) {
        if (section == 0) {
            return @"服务及收费";
        } else {
            return @"已验证作品";
        }
    } else {
        return @"已验证项目";
    }
    
   
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 50;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (_c == 0) {
        if (indexPath.section ==0 ) {
            return 40;
        } else {
            return kHeight / 2 + 110;
        }
    } else {
        return kHeight / 2 + 110;
    }

}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (_c == 0) {
        if (indexPath.section == 0) {
            
            WorkTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell12" forIndexPath:indexPath];
            
            cell.label.text = [self.array3 objectAtIndex:indexPath.row];
            
            cell.label1.text = [self.array4 objectAtIndex:indexPath.row];
            
            return cell;
            
        } else {
            
            BrowseThreeTableViewCell * browseCell = [tableView dequeueReusableCellWithIdentifier:@"cell11" forIndexPath:indexPath];
            
            BrowseModel *browseModel = _array[indexPath.row];
            
            browseCell.browseModel = browseModel;
            
            return  browseCell;
        }
    } else {
        
        BrowseThreeTableViewCell * browseCell = [tableView dequeueReusableCellWithIdentifier:@"cell11" forIndexPath:indexPath];
        
        BrowseModel *browseModel = _array[indexPath.row];
        
        browseCell.browseModel = browseModel;
        
        return  browseCell;
    }
   
}



//  点击单元格时
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
        DetailTowTableViewController *detailTwoVC = [[DetailTowTableViewController alloc]init];
    
        self.browseModel = _array[indexPath.row];
    
    
        detailTwoVC.browseModel = self.browseModel;
    
    [self presentViewController:detailTwoVC animated:YES completion:nil];
   
}























@end
